//
//  AppraisalViewController.m
//  PCSchool
//
//  Created by Arindam Ghosh on 7/12/13.
//  Copyright (c) 2013 Jayanta. All rights reserved.
//

#import "AppraisalViewController.h"

@interface AppraisalViewController ()

@end

@implementation AppraisalViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.goodBehaviourScrollView setContentSize:CGSizeMake(290.0, 1000.0)];
    
    

    
    NSLog(@"%@",NSStringFromClass(self.class));
    
    self.studentArray = [NSMutableArray array];
    
    
    [self.studentArray addObject:@"Adam 1"];
    [self.studentArray addObject:@"Adam 2"];
    [self.studentArray addObject:@"Adam 3"];
    [self.studentArray addObject:@"Adam 4"];
    [self.studentArray addObject:@"Adam 5"];
    [self.studentArray addObject:@"Adam 6"];
    [self.studentArray addObject:@"Adam 7"];
    [self.studentArray addObject:@"Adam 8"];
    [self.studentArray addObject:@"Adam 9"];
    [self.studentArray addObject:@"Adam 10"];
    [self.studentArray addObject:@"Adam 11"];
    [self.studentArray addObject:@"Adam 12"];
    [self.studentArray addObject:@"Adam 13"];
    [self.studentArray addObject:@"Adam 14"];
    [self.studentArray addObject:@"Adam 15"];
}

- (void)viewDidAppear:(BOOL)animated {
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [self.leftPanelTableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionBottom];
}


#pragma mark - TableView Delegates & Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        
        return 46.0;
    }
    if (tableView.tag == 102) {
        
        return 33.0;
    }
    if (tableView.tag == 103) {
        
        return 33.0;
    }
    
    return 0.0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 101) {
        return [self.studentArray count];
        
    }
    if (tableView.tag == 102) {
        
        return 8;
    }
    if (tableView.tag == 103) {
        return 8;

    }
    
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 101) {
        
        static NSString *CellIdentifier = @"StudenListCell";
        StudenListCell * cell =(StudenListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (!cell) {
            
            cell =(StudenListCell*)[BaseTableCell cellFromNibNamed:CellIdentifier];
            
            cell.cellImageView.userInteractionEnabled = YES;
            
            
            
            
        }
        
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
        longPress.delegate = self;
        [cell addGestureRecognizer:longPress];
        
        [self configureCell:cell atIndexPath:indexPath];
        
        return cell;
    }
    if (tableView.tag == 102) {
        
        static NSString *CellIdentifier = @"AppraisalTableCell";
        
         AppraisalTableCell* cell =(AppraisalTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (!cell) {
            
            cell =(AppraisalTableCell*)[BaseTableCell cellFromNibNamed:CellIdentifier];
            cell.cellImageView.image = [UIImage imageNamed:@"page5_subject_list_bg_A.png"];
            cell.cellImageView.highlightedImage = [UIImage imageNamed:@"page5_subject_list_bg_A.png"];
        }
        
        [self configureCell2:cell atIndexPath:indexPath];
        
        //[self configureCell2:cell atIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        return cell;
    }
    if (tableView.tag == 103) {
        
        static NSString *CellIdentifier = @"AppraisalTableCell";
        
        AppraisalTableCell* cell =(AppraisalTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (!cell) {
            
            cell =(AppraisalTableCell*)[BaseTableCell cellFromNibNamed:CellIdentifier];
            cell.cellImageView.image = [UIImage imageNamed:@"page5_subject_list_bg_bad_A.png"];
            cell.cellImageView.highlightedImage = [UIImage imageNamed:@"page5_subject_list_bg_bad_A.png"];
            
        }
        
        [self configureCell3:cell atIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        
        return cell;
    }
    
    return nil;
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


-(void)configureCell:(StudenListCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.cellTextLabel.text = [self.studentArray objectAtIndex:indexPath.row];
}

-(void)configureCell2:(AppraisalTableCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.cellTextLabel.text = [self.studentArray objectAtIndex:indexPath.row];

}

-(void)configureCell3:(AppraisalTableCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.cellTextLabel.text = [self.studentArray objectAtIndex:indexPath.row];

}

#pragma mark - Button Action

- (IBAction)proximityButtonAction:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    
    int selectedIndex = btn.selected ? 0 : 1;
    
    btn.selected = selectedIndex;
}

-(IBAction)goodBehaviourDeleteButtonAction:(id)sender
{
    
}

-(IBAction)goodBehaviourNotificationButtonAction:(id)sender
{
    
}

-(IBAction)goodBehaviourSubmitButtonAction:(id)sender
{
    
}

-(IBAction)badBehaviourDeleteButtonAction:(id)sender
{
    
}

-(IBAction)badBehaviourNotificationButtonAction:(id)sender
{
    
}

-(IBAction)badBehaviourSubmitButtonAction:(id)sender
{
    
}


#pragma mark - Drag and Drop Delegate

-(void)longGestureAction:(UILongPressGestureRecognizer *)gesture{
    StudenListCell *cell= (StudenListCell *)[gesture view];
    
    
    switch ([gesture state]) {
        case UIGestureRecognizerStateBegan:{
            
            NSIndexPath *ip = [self.leftPanelTableView indexPathForCell:cell];
            [self.leftPanelTableView setScrollEnabled:NO];
            if(ip!=nil){
                
                [self dragAndDropTableViewController:self.leftPanelTableView  draggingGestureWillBegin:gesture forCell:cell];
                
                UIView *draggedView = [self dragAndDropTableViewControllerView:self.leftPanelTableView ];
                //switch the view the gesture is associated with this will allow the dragged view to continue on where the cell leaves off from
                [draggedView addGestureRecognizer:[[cell gestureRecognizers]objectAtIndex:0]];
                
                [self dragAndDropTableViewController:self.leftPanelTableView draggingGestureDidBegin:gesture forCell:cell];
            }
        }
            break;
        case UIGestureRecognizerStateChanged:{
            [self dragAndDropTableViewController:self.leftPanelTableView draggingGestureDidMove:gesture];
        }
            break;
        case UIGestureRecognizerStateEnded:{
            UIView *draggedView = [self dragAndDropTableViewControllerView:self.leftPanelTableView];
            
            
            if(draggedView==nil)
                return;
            
            //might not be right to have both here but you need two different delegates so different controllers can controll different things
            [self dragAndDropTableViewController:self.leftPanelTableView draggingGestureDidEnd:gesture];
            [self dragAndDropTableViewController:self.leftPanelTableView droppedGesture:gesture];
            
            [self.leftPanelTableView setScrollEnabled:YES];
            [self.leftPanelTableView reloadData];
        }
            break;
            
            //        case UIGestureRecognizerStateCancelled:
            //        case UIGestureRecognizerStateFailed:
            //        case UIGestureRecognizerStatePossible:
            //            [self.dragAndDropDelegate dragAndDropTableViewController:self draggingGesture:gesture endedForItem:nil];
            break;
        default:
            break;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
        [gestureRecognizer addTarget:self action:@selector(longGestureAction:)];
    }
    
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
    return YES;
}


-(void)dragAndDropTableViewController:(UITableView *)ddtvc draggingGestureWillBegin:(UIGestureRecognizer *)gesture forCell:(StudenListCell *)cell{
    
    //AppraisalTableCell *tableCell = (AppraisalTableCell *)cell;
    
    NSLog(@"%@",cell.cellImageView);
    
    
    UIGraphicsBeginImageContext(cell.cellImageView.bounds.size);
    [cell.cellImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImageView *iv = [[UIImageView alloc] initWithImage:img];
    self.dragAndDropView = [[UIView alloc]initWithFrame:iv.frame];
    [self.dragAndDropView addSubview:iv];
    [self.dragAndDropView setBackgroundColor:[UIColor blueColor]];
    [self.dragAndDropView setCenter:[gesture locationInView:self.view.superview]];
    
    [self.view.superview addSubview:self.dragAndDropView];
}

-(void)dragAndDropTableViewController:(UITableView *)ddtvc draggingGestureDidBegin:(UIGestureRecognizer *)gesture forCell:(UITableViewCell *)cell;
{
    
    NSIndexPath *indexPath = [self.leftPanelTableView indexPathForCell:cell];
    self.selectedChoice =indexPath;//[self.selectedChoice objectAtIndex:indexPath.row];
    
   // [self.selectedChoices removeObjectAtIndex:[self.selectedChoices indexOfObject:self.selectedChoice]];
    
    //[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    
}

-(void)dragAndDropTableViewController:(UITableView *)ddtvc draggingGestureDidMove:(UIGestureRecognizer *)gesture{
    [self.dragAndDropView setCenter:[gesture locationInView:self.view.superview]];
}


-(void)dragAndDropTableViewController:(UITableView *)ddtvc draggingGestureDidEnd:(UIGestureRecognizer *)gesture{
    
    [self.dragAndDropView removeFromSuperview];
    self.dragAndDropView = nil;
}


-(UIView *)dragAndDropTableViewControllerView:(UITableView *)ddtvc{
    return self.dragAndDropView;
}

#pragma mark - DropableTableViewDelegate
-(void)dragAndDropTableViewController:(UITableView *)ddtvc droppedGesture:(UIGestureRecognizer *)gesture{
    
    UIView *viewHit = [self.view hitTest:[gesture locationInView:self.view.superview] withEvent:nil];
    
    
    StudenListCell *cell = (StudenListCell *)[self.leftPanelTableView cellForRowAtIndexPath:self.selectedChoice];
    
    NSLog(@"HIT  %@ >> %@ %d  %@",viewHit,cell.cellTextLabel.text,self.leftPanelTableView.indexPathForSelectedRow.row,self.selectedChoice);

    
    if ([viewHit isKindOfClass:[UIScrollView class]]) {
        
        [gesture.view setFrame:CGRectMake(5, 5, 20, 20)];
        [self.goodBehaviourScrollView addSubview:gesture.view];
        [self.goodBehaviourScrollView setNeedsDisplay];
    }
    
    
//    if([ddtvc isKindOfClass:[ChoicesTableViewController class]]){
//        ChoicesTableViewController *fromTBCV=(ChoicesTableViewController *)ddtvc;
//        id selectedChoice = fromTBCV.selectedChoice;
//        
//        if([fromTBCV.view isEqual:viewHit])
//            [fromTBCV.choices addObject:selectedChoice];
//        else if([viewHit.superview isKindOfClass:[UITableViewCell class]] && [fromTBCV.view isEqual:viewHit.superview.superview]){
//            //we have dropped on a cell in our table
//            NSIndexPath *ip = [fromTBCV.tableView indexPathForCell:(UITableViewCell *)viewHit.superview];
//            [fromTBCV.choices insertObject:selectedChoice atIndex:ip.row];
//        }
//        else if([self.selectedChoicesViewController.view isEqual:viewHit]){
//            [self.selectedChoicesViewController.selectedChoices addObject:selectedChoice];
//            [self.selectedChoicesViewController.tableView reloadData];
//        }
//        else if([viewHit.superview isKindOfClass:[UITableViewCell class]] && [self.selectedChoicesViewController.view isEqual:viewHit.superview.superview]){
//            //we have dropped on a cell in our table
//            NSIndexPath *ip = [self.selectedChoicesViewController.tableView indexPathForCell:(UITableViewCell *)viewHit.superview];
//            [self.selectedChoicesViewController.selectedChoices insertObject:selectedChoice atIndex:ip.row];
//            [self.selectedChoicesViewController.tableView reloadData];
//        }
//        
//        
//    }
//    else if([ddtvc isKindOfClass:[SelectedChoicesTableViewController class]]){
//        SelectedChoicesTableViewController *fromSCTVC=(SelectedChoicesTableViewController *)ddtvc;
//        id selectedChoice = fromSCTVC.selectedChoice;
//        
//        if([fromSCTVC.view isEqual:viewHit])
//            [fromSCTVC.selectedChoices addObject:selectedChoice];
//        else if([viewHit.superview isKindOfClass:[UITableViewCell class]] && [fromSCTVC.view isEqual:viewHit.superview.superview]){
//            //we have dropped on a cell in our table
//            NSIndexPath *ip = [fromSCTVC.tableView indexPathForCell:(UITableViewCell *)viewHit.superview];
//            [fromSCTVC.selectedChoices insertObject:selectedChoice atIndex:ip.row];
//        }
//        else if([self.choicesViewController.view isEqual:viewHit]){
//            [self.choicesViewController.choices addObject:selectedChoice];
//            [self.choicesViewController.tableView reloadData];
//        }
//        else if([viewHit.superview isKindOfClass:[UITableViewCell class]] && [self.choicesViewController.view isEqual:viewHit.superview.superview]){
//            //we have dropped on a cell in our table
//            NSIndexPath *ip = [self.choicesViewController.tableView indexPathForCell:(UITableViewCell *)viewHit.superview];
//            [self.choicesViewController.choices insertObject:selectedChoice atIndex:ip.row];
//            [self.choicesViewController.tableView reloadData];
//        }
//        
//    }
//    else
//        NSLog(@"Item dropped on a non droppable area");
}



#pragma mark - Memory CleanUp

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    
    
    [_baseImageView release];
    [_rightPanelImageView release];
    [_leftPanelView release];
    [_leftPanelImageView release];
    [_leftPanelTableView release];
    [_searchBarImgView release];
    [_searchBarTextField release];
    [_proximityButton release];
    [_searchBarLeftimageView release];

    [_studentArray release];
    
    [_rightPanelLeftTableView release];
    [_rightPanelRightTableView release];
    
    [_rightPanelLeftImageView release];
    [_rightPanelRightImageView release];
    
    [_rightPanelLeftTextField release];
    [_rightPanelRightTextField release];
    
    [_rightPanelLeftTextView release];
    [_rightPanelRightTextView release];
    
    [_goodBehaviourDeleteButton release];
    [_goodBehaviourNotificationButton release];
    [_goodBehaviourSubmitButton release];
    [_badBehaviourDeleteButton release];
    [_badBehaviourNotificationButton release];
    [_badBehaviourSubmitButton release];
    
    
    [_goodBehaviourScrollView release];
    [_badBehaviourScrollView release];
    
    [super dealloc];
}

@end
